/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.tosapi.constant.enums;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 在这里编写类的功能描述
 *
 * @author duanlikao
 * @created 2021/7/17
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public enum BizCodeEnum {
    SUCCESS("SUCCESS", "成功"),
    BAD_REQUEST("BAD_REQUEST", "请求错误"),
    NEED_AUTHORIZATION("NEED_AUTHORIZATION", "没有权限"),
    NEED_LOGIN("NEED_LOGIN", "需要登录"),
    NOT_FOUND("NOT_FOUND", "未找到"),
    INVALID_STATUS("INVALID_STATUS", "无效状态"),
    INVALID_ACTION("INVALID_ACTION", "无效操作"),
    INVALID_SERVICE("INVALID_SERVICE", "无效的服务"),
    INVALID_SIGNATURE("INVALID_SIGNATURE", "无效的签名"),
    SERVICE_ERROR("SERVICE_ERROR", "渠道服务错误"),
    TIMEOUT("TIMEOUT", "超时"),
    UNKNOWN_ERROR("UNKNOWN_ERROR", "未知错误"),
    INTERNAL_SERVER_ERROR("INTERNAL_SERVER_ERROR", "内部服务异常"),
    ;

    private  String code;
    private  String msg;


    private static Map <String, BizCodeEnum> codeMap = Maps.newHashMap();


    public String getCode() {
        return this.code;
    }

    public String getDesc() {
        return this.msg;
    }


    public static String getMsg(String code) {
        return codeMap.containsKey(code) ? codeMap.get(code).getDesc() : StringUtils.EMPTY;
    }

    public static boolean isValid(String code) {
        return codeMap.containsKey(code);
    }

    static {
        for (BizCodeEnum status : BizCodeEnum.values()) {
            codeMap.put(status.getCode(), status);
        }
    }

    public static BizCodeEnum getBizCodeEnumByVal(String code) {
        return Arrays.stream(BizCodeEnum.values()).collect(Collectors.toMap(
                BizCodeEnum::getCode, value -> value, (k1, k2) -> k2)).get(code);
    }
}